<?php

use yii\db\Migration;

class m170422_115832_tables extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%product}}', [
            'id' => $this->bigPrimaryKey()->unsigned()->comment('产品 ID'),
            'series_id' => $this->bigInteger()->unsigned()->comment('系列 ID'),
            'name' => $this->string()->notNull()->comment('名称'),
            'preview' => $this->string()->notNull()->comment('主图'),
            'created_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('创建时间'),
            'updated_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('修改时间'),
            'status' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('状态'),
        ], $tableOptions . ' COMMENT="产品"');

        $this->addPrimaryKey('Product Attribute', '{{%product_attribute}}', ['product_id', 'attr_type_id', 'attr_id']);

        $this->createTable('{{%product_gallery}}', [
            'product_id' => $this->bigInteger()->unsigned()->comment('产品 ID'),
            'file' => $this->string()->notNull()->comment('图片'),
        ], $tableOptions . ' COMMENT="产品组图"');

        $this->addPrimaryKey('Product Gallery', '{{%product_gallery}}', ['product_id', 'file']);

        $this->createTable('{{%case}}', [
            'id' => $this->bigPrimaryKey()->unsigned()->comment('案例 ID'),
            'name' => $this->string()->notNull()->comment('名称'),
            'preview' => $this->string()->notNull()->comment('主图'),
            'developer' => $this->string()->notNull()->defaultValue('')->comment('开发商'),
            'age' => $this->string(4)->notNull()->defaultValue('')->comment('年代'),
            'created_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('创建时间'),
            'updated_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('修改时间'),
            'status' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('状态'),
        ], $tableOptions . ' COMMENT="案例"');

        $this->createTable('{{%case_product}}', [
            'case_id' => $this->bigInteger()->unsigned()->comment('产品属性 ID'),
            'product_id' => $this->bigInteger()->unsigned()->comment('产品 ID'),
        ], $tableOptions . ' COMMENT="案例产品关联"');

        $this->addPrimaryKey('Case Product', '{{%case_product}}', ['case_id', 'product_id']);

        $this->createTable('{{%case_gallery}}', [
            'case_id' => $this->bigInteger()->unsigned()->comment('案例 ID'),
            'file' => $this->string()->notNull()->comment('图片'),
        ], $tableOptions . ' COMMENT="案例组图"');

        $this->addPrimaryKey('Case Gallery', '{{%case_gallery}}', ['case_id', 'file']);

        $this->createTable('{{%timeline}}', [
            'id' => $this->bigPrimaryKey()->unsigned()->comment('事记 ID'),
            'case_id' => $this->bigInteger()->unsigned()->defaultValue(0)->comment('案例 ID'),
            'name' => $this->string()->notNull()->comment('标题'),
            'content' => $this->text()->comment('内容'),
            'created_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('创建时间'),
            'updated_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('修改时间'),
            'status' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('状态'),
        ], $tableOptions . ' COMMENT="事记"');

        $this->createTable('{{%cart}}', [
            'user_id' => $this->bigInteger()->unsigned()->comment('用户 ID'),
            'product_id' => $this->bigInteger()->unsigned()->comment('产品 ID'),
            'quantity' => $this->bigInteger()->unsigned()->defaultValue(0)->comment('数量'),
            'data' => $this->text()->notNull()->comment('数据'),
        ], $tableOptions . ' COMMENT="事记"');

        $this->addPrimaryKey('Cart', '{{%cart}}', ['user_id', 'product_id']);

        $this->createTable('{{%order}}', [
            'id' => $this->bigInteger()->unsigned()->comment('订单 ID'),
            'user_id' => $this->bigInteger()->unsigned()->comment('用户 ID'),
            'created_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('创建时间'),
            'updated_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('修改时间'),
            'status' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('状态'),
        ], $tableOptions . ' COMMENT="事记"');

        $this->addPrimaryKey('Order Id', '{{%order}}', 'id');

        $this->createTable('{{%order_product}}', [
            'order_id' => $this->bigInteger()->unsigned()->comment('产品属性 ID'),
            'product_id' => $this->bigInteger()->unsigned()->comment('产品 ID'),
            'quantity' => $this->bigInteger()->unsigned()->defaultValue(0)->comment('数量'),
            'data' => $this->text()->notNull()->comment('数据'),
            'status' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('状态'),
        ], $tableOptions . ' COMMENT="订单产品关联"');

        $this->addPrimaryKey('Order Product Id', '{{%order_product}}', ['order_id', 'product_id']);

        $this->createTable('{{%user}}', [
            'id' => $this->bigPrimaryKey()->unsigned()->comment('用户 ID'),
            'username' => $this->string(30)->notNull()->unique()->comment('用户名'),
            'auth_key' => $this->string(32)->notNull()->comment('密钥'),
            'password_hash' => $this->string()->notNull()->comment('加密密码'),
            'password_reset_token' => $this->string()->unique()->comment('密码重置 Token'),
            'email' => $this->string(90)->unique()->comment('邮箱'),
            'mobile' => $this->string(30)->notNull()->unique()->comment('手机'),
            'created_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('创建时间'),
            'updated_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('修改时间'),
            'is_admin' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('是否管理员'),
            'status' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('状态'),
        ], $tableOptions . ' COMMENT="用户"');

        $this->createIndex('is-admin', '{{%user}}', 'is_admin');
        $this->createIndex('user-status', '{{%user}}', 'status');

        $this->createTable('{{%menu}}', [
            'id' => $this->bigPrimaryKey()->unsigned()->comment('菜单 ID'),
            'parent_id' => $this->bigInteger(20)->notNull()->defaultValue(0)->comment('父级'),
            'name' => $this->string(30)->notNull()->comment('名称'),
            'icon' => $this->string(30)->notNull()->defaultValue('')->comment('图标'),
            'child' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('有子级'),
            'parent_arr' => $this->string()->notNull()->defaultValue(0)->comment('父级链'),
            'child_arr' => $this->text()->comment('子级群'),
            'controller' => $this->string(30)->notNull()->defaultValue('')->comment('控制器'),
            'action' => $this->string(30)->notNull()->defaultValue('')->comment('方法'),
            'params' => $this->string(255)->notNull()->defaultValue('')->comment('参数'),
            'auth_item' => $this->string(30)->notNull()->defaultValue('')->comment('权限'),
            'sort' => $this->bigInteger()->unsigned()->notNull()->defaultValue(0)->comment('排序'),
            'status' => $this->smallInteger(1)->notNull()->defaultValue(0)->comment('状态'),
        ], $tableOptions . " COMMENT='菜单'");

        $this->createIndex('Parent Id', '{{%menu}}', 'parent_id');
        $this->createIndex('Status', '{{%menu}}', 'status');

        $this->createTable('{{%ad}}', [
            'id' => $this->bigPrimaryKey()->unsigned()->comment('广告 ID'),
            'name' => $this->string()->notNull()->comment('名称'),
            'preview' => $this->string()->notNull()->comment('主图'),
            'url' => $this->string()->notNull()->defaultValue('')->comment('链接'),
            'created_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('创建时间'),
            'updated_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('修改时间'),
            'status' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('状态'),
        ], $tableOptions . ' COMMENT="广告"');

        $this->createTable('{{%partner}}', [
            'id' => $this->bigPrimaryKey()->unsigned()->comment('伙伴 ID'),
            'name' => $this->string()->notNull()->comment('名称'),
            'preview' => $this->string()->notNull()->comment('主图'),
            'url' => $this->string()->notNull()->defaultValue('')->comment('链接'),
            'created_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('创建时间'),
            'updated_at' => $this->integer()->unsigned()->notNull()->defaultValue(0)->comment('修改时间'),
            'status' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('状态'),
        ], $tableOptions . ' COMMENT="合作伙伴"');
    }

    public function down()
    {
        $this->dropTable('{{%product}}');
        $this->dropTable('{{%product_gallery}}');
        $this->dropTable('{{%case}}');
        $this->dropTable('{{%case_product}}');
        $this->dropTable('{{%case_gallery}}');
        $this->dropTable('{{%timeline}}');
        $this->dropTable('{{%cart}}');
        $this->dropTable('{{%order}}');
        $this->dropTable('{{%order_product}}');
        $this->dropTable('{{%user}}');
        $this->dropTable('{{%menu}}');
        $this->dropTable('{{%ad}}');
        $this->dropTable('{{%partner}}');
    }
}
